<?php
namespace app\api\controller;
use app\api\controller\BaseController;
use app\common\errcode\ApiErrCode;
use app\common\errcode\ApiErrMsg;
use think\Loader;

class TaskController extends BaseController
{
    protected $taskModel;

    public function __construct()
    {
        parent::__construct();
        $this->taskModel = new \app\api\model\TaskModel();
    }

    /**
     * 创建任务
     * 
     */
    public function create()
    {
        try {
            if($this->request->isPost()){
                $data = $this->request->post();
                $validate = Loader::validate('Task');
                $check = $validate->scene('create')->check($data);
                if(!$check) return $this->jsonJump(ApiErrCode::PARAM_ERROR,ApiErrMsg::PARAM_ERROR.','.$validate->getError());
                if($this->taskModel->addTask($this->userid,$this->departmentId,$data)){
                    return $this->jsonJump(ApiErrCode::SUCCESS,ApiErrMsg::SUCCESS);
                }
                return $this->jsonJump(ApiErrCode::CREATE_TASK_FAIL,ApiErrMsg::CREATE_TASK_FAIL);    
            }
        } catch (\Exception $e) {
            \app\common\model\CommonModel::savelog($this->userid,$e,$this->request->path());
            return $this->jsonJump(ApiErrCode::SYSTEM_ERROR,ApiErrMsg::SYSTEM_ERROR . ',错误信息:'. $e->getMessage());
        }    
    }

    /**
     * 任务列表
     * @param int       $pagesize
     * @param int       $page
     * @param string    $create_userid
     * @param string    $type
     * @param int       $status
     * @param int       $important
     * @param string    $finish_date_start
     * @param string    $finish_date_end
     * @param int       $customer_id
     * @param int       $brand_id
     * @param int       $product_id
     * @return JSON
     */
    public function index()
    {
        try {
            if($this->request->isPost()){
                $data = $this->request->post();
                return $this->jsonJump(ApiErrCode::SUCCESS,ApiErrMsg::SUCCESS,$this->taskModel->getList($this->userid,$this->departmentId,$data));         
            }
        } catch (\Exception $e) {
            \app\common\model\CommonModel::savelog($this->userid,$e,$this->request->path());
            return $this->jsonJump(ApiErrCode::SYSTEM_ERROR,ApiErrMsg::SYSTEM_ERROR . ',错误信息:'. $e->getMessage());
        }
    }

    /**
     * 任务详情
     * @param string $tid
     */
    public function info()
    {
        try {
            if($this->request->isPost()){
                $data = $this->request->post();
                $validate = Loader::validate('Task');
                $check = $validate->scene('info')->check($data);
                if(!$check) return $this->jsonJump(ApiErrCode::PARAM_ERROR,ApiErrMsg::PARAM_ERROR.','.$validate->getError());
                return $this->jsonJump(ApiErrCode::SUCCESS,ApiErrMsg::SUCCESS,$this->taskModel->getInfo($this->userid,$this->departmentId,$data));
            }    
        } catch (\Exception $e) {
            \app\common\model\CommonModel::savelog($this->userid,$e,$this->request->path());
            return $this->jsonJump(ApiErrCode::SYSTEM_ERROR,ApiErrMsg::SYSTEM_ERROR . ',错误信息:'. $e->getMessage());
        }    
    }

    /**
     * 删除任务
     * @param string $tid
     * 
     * @return JSON
     */
    public function delete()
    {
        try {
            if($this->request->isPost()){
                $data = $this->request->post();
                $validate = Loader::validate('Task');
                $check = $validate->scene('delete')->check($data);
                if(!$check) return $this->jsonJump(ApiErrCode::PARAM_ERROR,ApiErrMsg::PARAM_ERROR.','.$validate->getError());
                if($this->taskModel->deleteTask($this->userid,$this->departmentId,$data)){
                    return $this->jsonJump(ApiErrCode::SUCCESS,ApiErrMsg::SUCCESS);
                }
                return $this->jsonJump(ApiErrCode::DELETE_TASK_FAIL,ApiErrMsg::DELETE_TASK_FAIL);             
            }    
        } catch (\Exception $e) {
            \app\common\model\CommonModel::savelog($this->userid,$e,$this->request->path());
            return $this->jsonJump(ApiErrCode::SYSTEM_ERROR,ApiErrMsg::SYSTEM_ERROR . ',错误信息:'. $e->getMessage());
        }
    }

    /**
     * 任务操作
     * @param string $method    操作类型
     * @param string 
     */
    public function edit()
    {
        try {
            if($this->request->isPost()){
                $data = $this->request->post();
                $validate = Loader::validate('Task');
                $check = $validate->scene('handle')->check($data);
                if(!$check) return $this->jsonJump(ApiErrCode::PARAM_ERROR,ApiErrMsg::PARAM_ERROR.','.$validate->getError());
                if($this->taskModel->handleTask($this->userid,$this->departmentId,$data)){
                    return $this->jsonJump(ApiErrCode::SUCCESS,ApiErrMsg::SUCCESS);
                }
                return $this->jsonJump(ApiErrCode::HANDLE_TASK_FAIL,ApiErrMsg::HANDLE_TASK_FAIL);         
            }
        } catch (\Exception $e) {
            \app\common\model\CommonModel::savelog($this->userid,$e,$this->request->path());
            return $this->jsonJump(ApiErrCode::SYSTEM_ERROR,ApiErrMsg::SYSTEM_ERROR . ',错误信息:'. $e->getMessage());
        }        
    }

    public function overview()
    {
        try {
            if($this->request->isPost()){
                return $this->jsonJump(ApiErrCode::SUCCESS,ApiErrMsg::SUCCESS,$this->taskModel->getOverview($this->userid, $this->departmentId));    
            }    
        } catch (\Exception $e) {
            \app\common\model\CommonModel::savelog($this->userid,$e,$this->request->path());
            return $this->jsonJump(ApiErrCode::SYSTEM_ERROR,ApiErrMsg::SYSTEM_ERROR . ',错误信息:'. $e->getMessage());
        }
    }
}